        var saleStore = new Ext.data.JsonStore({
			url: '/weihe/getDTCargo_saleList',
			root: 'datas',
			fields : [{
	        	   name:'saleId',mapping:'id'
	          },{
	        	   name:'saleAmount',mapping:'amount' 
	          },{
				   name:'salePrice',mapping:'price'
			  },{
				   name:'saleRemark',mapping:'remark'
			  },{
				   name:'saleSum',mapping:'sum'
			  },{
				   name:'cargoId',mapping:'cargo', convert:function(v){ 
												return v ? v.id : undefined;}
			  },{
				   name:'saleLocation',mapping:'cargo', convert:function(v){ 
												return v ? v.location : undefined;}
			  },{
				   name:'salePlace',mapping:'cargo', convert:function(v){ 
												return v ? v.place : undefined;}
			  },{
				   name:'saleCode',mapping:'cargo', convert:function(v){ 
												return v ? v.code : undefined;}
	          },{
	        	   name:'saleName',mapping:'cargo', convert:function(v){ 
												return v ? v.name : undefined;}
	          },{
	        	   name:'saleSpecs',mapping:'cargo', convert:function(v){ 
	        	  								return v ? v.specs : undefined;}
	          },{
	        	   name:'saleUnit',mapping:'cargo', convert:function(v){ 
	        	  								return v ? v.unit : undefined;}
	          }],
					listeners :{ 
					   'beforeload':function(s,options) {
							var tag = 0;
							Ext.Ajax.request({
								url : '/weihe/getListJudge_saleList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									if (json.deny) {
										Ext.Msg.alert('错误',json.msg);
										tag = 1;
									}
										if(tag == 1){
											return false;
										}else{
											return true;						
										}
								},
								failure : function() {
									Ext.Msg.alert('错误', '失败!');
									tag =1;
								}
							});
			   }
			}

		});
        
        var saleListStore = new Ext.data.JsonStore({
			url: '/weihe/getList_saleList',
			root: 'datas',
			fields : [{
			        	   name:'saleListId',mapping:'id'
			          },{
			        	   name:'saleOrderNum',mapping:'orderNum' 
			          },{
        				   name:'saleListNum',mapping:'listNum'
        			  },{
        				   name:'saleDate', mapping:'date',type : 'date', dateFormat:"Y-m-d"
        			  },{
        				   name:'saleDepartment',mapping:'department'
        			  },{
        				   name:'saleType',mapping:'type'
        			  },{
        				   name:'saleListRemark',mapping:'remark'
        			  },{
        				   name:'saleWarehouse',mapping:'warehouse'
        			  },{
        				   name:'saleMaker',mapping:'maker'
        			  },{
        				   name:'saleAuditor',mapping:'auditor'
        			  },{
        				   name:'saleClientName',mapping:'clientName'
        			  },{
        				   name:'saleSalesMan',mapping:'salesMan'
        			  },{
        				   name:'saleSentAddress',mapping:'sentAddress'
        			  },{
        				   name:'saleSentWay',mapping:'sentWay'
        			  }],
					listeners :{ 
					   'beforeload':function(s,options) {
							var tag = 0;
							Ext.Ajax.request({
								url : '/weihe/getListJudge_saleList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									if (json.deny) {
										Ext.Msg.alert('错误',json.msg);
										tag = 1;
									}
										if(tag == 1){
											return false;
										}else{
											return true;						
										}
								},
								failure : function() {
									Ext.Msg.alert('错误', '失败!');
									tag =1;
								}
							});
			   }
			}

		});
    	
    	
    
    	

sales = function(){
  return{

	  
	  
	  listPanel : function(){
		  
			var listPanel = new Ext.Panel( {
				                    region : 'north',
				                    layout : 'border',
				                    frame  : true,
				                    height : 230,
				                    items  : [sales.listGrid(),sales.listForm()]
			                      });
		  
		  return listPanel;
	  	},

	  	  listForm : function(){
		  
			var button_add = new Ext.Button({
				text : '添加',
				handler : function() {
					progressConsole();
					if(listForm.form.isValid()){ 
						var tmp = listForm.form.findField('saleList.warehouse').getValue();
						Ext.getCmp('saleCode').getStore().setBaseParam('location',tmp);
						Ext.getCmp('saleCode').getStore().load({params : {query : '%'}});
						Ext.getCmp('saleName').getStore().setBaseParam('location',tmp);
						Ext.getCmp('saleName').getStore().load({params : {query : '%'}});
						Ext.getCmp("salesForm").buttons[0].enable();
						Ext.getCmp("salesForm").buttons[1].enable();
						Ext.getCmp("salesForm").buttons[2].enable();
						if (listForm.form.findField("saleListId").getValue() == "") {
							 listForm.form.submit({
							     url : '/weihe/add_saleList',
							     success : function(form, action) {
								       if (action.result.deny) {
									      Ext.Msg.alert('错误', action.result.msg);
								       }else{
									        Ext.Msg.alert('消息', action.result.msg, function() {
									        listForm.form.reset();
									        Ext.getCmp("sgrid").getStore().removeAll();
										    Ext.getCmp("salesForm").form.findField('saleList.id').setValue(action.result.saleList_id);
										    Ext.getCmp("saleListGrid").getStore().load({params : {start : action.result.total-1, limit : 6}});;
									        })
								       }
							     },
								 failure : function(form, action) {
		      							Ext.Msg.alert('错误', action.result.msg);
		      						}
						      })
						}else {
								listForm.form.submit({
									url : '/weihe/update_saleList',
									success : function(form, action) {
										 if (action.result.deny) {
											 Ext.Msg.alert('错误', action.result.msg);
								 		 }else{
											Ext.Msg.alert('消息', action.result.msg, function() {
												Ext.getCmp("saleListGrid").getStore().reload();
											})
										}
								   },
									 failure : function(form, action) {
		      							Ext.Msg.alert('错误', action.result.msg);
		      						}
								})
						}
					}else{
						Ext.Msg.alert('提示','请输入相关内容');
					}
				}
			});

		var button_clear = new Ext.Button({
			text : '清空',
			handler : function() {
			
			listForm.form.reset();
			listForm.buttons[0].setText('添加');
			Ext.getCmp("salesForm").form.reset();
			Ext.getCmp("salesForm").buttons[0].disable();
			Ext.getCmp("salesForm").buttons[1].disable();
			Ext.getCmp("salesForm").buttons[2].disable();
			Ext.getCmp("sgrid").getStore().removeAll();
			}
		});

		var button_del = new Ext.Button({
			text : '删除',
			handler : function() {
				var id = listForm.form.findField('saleListId').getValue();
				if (id == '') {
					Ext.Msg.alert('提示', '请选择需要删除的信息!');
				} else {
					progressConsole();
					Ext.Ajax.request({
						url : '/weihe/delete_saleList',
						success : function(response) {
							var json = Ext.decode(response.responseText);
							if (json.success) {
							  if (json.deny) {
									Ext.Msg.alert('错误',json.msg);
									listForm.form.reset();
									listForm.buttons[0].setText('添加');
							 }else{
								Ext.Msg.alert('消息', json.msg, function() {
									Ext.getCmp("saleListGrid").getStore().load({params : {start : 0, limit : 6}});
									listForm.form.reset();
									listForm.buttons[0].setText('添加');
								});
							  } 
							}else{
								Ext.Msg.alert('消息', json.msg);
							}
						},
						failure : function() {
							Ext.Msg.alert('错误', '删除失败!');
						},
						params: { 'saleList.id' : listForm.form.findField('saleListId').getValue() }
					});
				}
			}
		});
		
		  
		  var whComboData = new Ext.data.SimpleStore({
				fields : ['text'],
				data : [['A'],['B'],['C']]
			});
		  
		  var typeComboData = new Ext.data.SimpleStore({
				fields : ['text'],
				data : [['发运'],['销售']]
			});
		  
				var listForm = new Ext.form.FormPanel({
				id : 'saleListForm',
				title : '产品销售信息',
				region : 'east',
				frame : true,
				width : 300,
				autoScroll : true,
				collapseMode :'mini',
				collapsible : true,
				labelAlign : 'right',
				labelWidth : 60,
				defaultType : 'textfield',
					defaults : {
					width : 200
				},
				items : [{
					id: 'saleListId',
					xtype : 'hidden',
					name : 'saleList.id'
				},{
					id: 'saleListNum',
					xtype : 'hidden',
					name : 'saleList.listNum'
				},{
					id: 'saleOrderNum',
					fieldLabel: "订单号",
	        	    name: "saleList.orderNum"
				},{
					id: 'saleDate',
	                xtype: 'datefield',
	                fieldLabel: "销售日期", 
	                format : 'Y-m-d',
	  			    allowBlank:false,
	                name: "saleList.date"
				},{
				    id: 'saleType',
				    fieldLabel: "销售类别",
	                name: "saleList.type",
				    xtype : 'combo',
				    store : typeComboData,
				    emptyText : '请选择',
					mode : 'local',
					triggerAction : 'all',
					allowBlank:false,
					editable: false,
					displayField : 'text'
				},{
					id:'saleWarehouse',
					fieldLabel : '仓库',
					name : 'saleList.warehouse',
					xtype : 'combo',
					store : whComboData,
					emptyText : '请选择',
					mode : 'local',
					triggerAction : 'all',
					allowBlank:false,
					editable: false,
					displayField : 'text'
				},{
					id: 'saleDepartment',
					 fieldLabel: "销售部门",
	             name: "saleList.department"
				},{
					id: 'saleListRemark',
					 fieldLabel: "备注",
	              name: "saleList.remark"
				},{
					id: 'saleClientName',
					 fieldLabel: "客户名称",
	              name: "saleList.clientName"
				},{
					id: 'saleSentAddress',
					 fieldLabel: "发送地址",
	              name: "saleList.sentAddress"
				},{
					id: 'saleSentWay',
					 fieldLabel: "发送方式",
	              name: "saleList.sentWay"
				},{
					id: 'saleSalesMan',
					 fieldLabel: "业务员",
	              name: "saleList.salesMan"
				},{
					id: 'saleMaker',
					 fieldLabel: "制单人",
	             name: "saleList.maker"
				}],
				buttons : [button_add,button_clear,button_del]
		});
		  
		  return listForm;
	},  
		  
		  
	listGrid : function(){
		  
			var pageToolBar = new Ext.PagingToolbar({
				pageSize : 6,
				store : saleListStore,
				displayInfo : true,
				displayMsg : '当前显示{0}-{1}条记录,共{2}条记录',
			    emptyMsg : "没有记录显示!",
			    firstText : '首页',
			    prevText : '上一页',
			    nextText : '下一页',
			    lastText : '尾页',
			    beforePageText : '第',
			    afterPageText : '页,共{0}页'
			});


			var rowSelectionModel = new Ext.grid.RowSelectionModel({singleSelect : true});
			
			   
		   var listGrid = new Ext.grid.GridPanel({
			   id:'saleListGrid',
			   region : 'center',
			   minColumnWidth : 35,
			   loadMask : {msg:'正在加载数据，请稍侯...'},
			   store : saleListStore,
			   columns: [
			             {header : '订单号', dataIndex : 'saleOrderNum'},
			             {header : '销售单号', dataIndex : 'saleListNum'},
			             {header : '销售日期', dataIndex : 'saleDate',sortable : true,renderer:Ext.util.Format.dateRenderer('Y年m月d日')},
			             {header : '销售类别', dataIndex : 'saleType'},
			             {header : '销售部门', dataIndex : 'saleDepartment'},
			             {header : '仓库', dataIndex : 'saleWarehouse'},
			             {header : '客户名称', dataIndex : 'saleClientName'},
			             {header : '发送地址', dataIndex : 'saleSentAddress'},
			             {header : '发送方式', dataIndex : 'saleSentAddress'},
			             {header : '业务员', dataIndex : 'saleSalesMan'},
			             {header : '备注', dataIndex : 'saleListRemark'},
			             {header : '制单人', dataIndex : 'saleMaker'},
			             {header : '审核人', dataIndex : 'saleAuditor'}
			             ],
	       sm : rowSelectionModel,
	       listeners :{ 
				   'rowclick':function(grid, rowIndex, event) {
				   		var record = grid.getStore().getAt(rowIndex);
				   		Ext.getCmp("saleListForm").form.loadRecord(record);
				   		Ext.getCmp("salesForm").form.findField('saleList.id').setValue(record.id);
				   		Ext.getCmp("salesForm").buttons[0].enable();
						Ext.getCmp("salesForm").buttons[1].enable();
						Ext.getCmp("salesForm").buttons[2].enable();
						Ext.getCmp("saleListForm").buttons[0].setText('修改');
						Ext.getCmp('saleCode').getStore().setBaseParam('location',record.get('saleWarehouse'));
						Ext.getCmp('saleCode').getStore().load({params : {query : '%'}});
						Ext.getCmp('saleName').getStore().setBaseParam('location',record.get('saleWarehouse'));
						Ext.getCmp('saleName').getStore().load({params : {query : '%'}});
						Ext.getCmp('saleCode').getStore().baseParams = {'location': record.get('saleWarehouse')};
				   		Ext.getCmp('saleName').getStore().baseParams = {'location': record.get('saleWarehouse')};
						Ext.getCmp("sgrid").getStore().baseParams = {'saleList.id': record.id};
						Ext.getCmp("sgrid").getStore().load({params : {start : 0, limit : 6}});
			   		}
			   },
			  bbar : pageToolBar
	});
			return listGrid;
		},
		  
		
    grid : function(){
	 


		var pageToolBar = new Ext.PagingToolbar({
			pageSize : 6,
			store : saleStore,
			displayInfo : true,
			displayMsg : '当前显示{0}-{1}条记录,共{2}条记录',
		    emptyMsg : "没有记录显示!",
		    firstText : '首页',
		    prevText : '上一页',
		    nextText : '下一页',
		    lastText : '尾页',
		    beforePageText : '第',
		    afterPageText : '页,共{0}页'
		   


		});


		var rowSelectionModel = new Ext.grid.RowSelectionModel({singleSelect : true});
		
		   
	   var grid = new Ext.grid.GridPanel({
		   id:'sgrid',
		   region : 'center',
		   minColumnWidth : 50,
		   loadMask : {msg:'正在加载数据，请稍侯...'}, // 单击分页时显示,为true显示'Loading...'
		   store : saleStore,
		   columns: [
		             {header : '产品编号', dataIndex : 'saleCode', menuDisabled : true},
		             {header : '产品名称', dataIndex : 'saleName'},
		             {header : '规格型号', dataIndex : 'saleSpecs', menuDisabled : true},
		             {header : '单位', dataIndex : 'saleUnit', menuDisabled : true},
		             {header : '所在仓库', dataIndex : 'saleLocation', menuDisabled : true},
		             {header : '存放位置', dataIndex : 'salePlace', menuDisabled : true},
		             {header : '数量', dataIndex : 'saleAmount', menuDisabled : true},
		             {header : '单价', dataIndex : 'salePrice',sortable : true},
		             {header : '金额', dataIndex : 'saleSum',sortable : true},
		             {header : '备注', dataIndex : 'saleRemark', menuDisabled : true}
		             
        ],
           sm : rowSelectionModel,
           listeners :{ 
			   'rowclick':function(grid, rowIndex, event) {
			   var record = grid.getStore().getAt(rowIndex);
			   Ext.getCmp("salesForm").form.loadRecord(record);
			   Ext.getCmp("salesForm").buttons[0].setText('修改');
			  
		   }
		},
		  viewConfig : {
		     forceFit : false
		  },
		  bbar : pageToolBar
});
		return grid;
	},
	salesForm : function(){
		
		var codestore = new Ext.data.JsonStore({
			url: '/weihe/getCargosByCode_depotList',
			root: 'codes',
			totalProperty: 'count',
			fields: ['code']
		});
		
		var namestore = new Ext.data.JsonStore({
			url: '/weihe/getCargosByname_depotList',
			root: 'names',
			totalProperty: 'count',
			fields: ['name']
		});
		
		var codeCombo = {
				id : 'saleCode',
				xtype: 'combo',
				fieldLabel: '产品编号',
				store: codestore,
				typeAhead: false,
				name : 'cargo.code',
				loadingText: '查找中...',
				emptyText: '请输入3位以上编号', 
				minChars: 3,
				pageSize: 0,
				hideTrigger: true,
				editable: true,
				triggerAction: 'all',
				displayField : 'code',
				listeners :{ 'select':function(combo, record,index){
							Ext.Ajax.request({
								url : '/weihe/getCode_depotList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									  salesForm.form.findField('saleName').setValue(json.name);
									  salesForm.form.findField('saleSpecs').setValue(json.specs);
									  salesForm.form.findField('saleUnit').setValue(json.unit);
									  salesForm.form.findField('salePrice').setValue(json.price);
									  salesForm.form.findField('salePlace').setValue(json.place);
									  salesForm.form.findField('cargo.id').setValue(json.id);
									},
								params : 'query=' + record.get('code')
							});
				    }
				}
		}
		var nameCombo = {
				id : 'saleName',
				xtype: 'combo',
				fieldLabel: '产品名称',
				store: namestore,
				typeAhead: false,
				name : 'cargo.name',
				loadingText: '查找中...',
				emptyText: '请输入产品名称', 
				minChars: 2,
				pageSize: 0,
				hideTrigger: true,
				editable: true,
				triggerAction: 'all',
				displayField : 'name',
				listeners :{ 'select':function(combo, record,index){
							Ext.Ajax.request({
								url : '/weihe/getName_depotList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									 salesForm.form.findField('saleCode').setValue(json.code);
									  salesForm.form.findField('saleSpecs').setValue(json.specs);
									  salesForm.form.findField('saleUnit').setValue(json.unit);
									  salesForm.form.findField('salePrice').setValue(json.price);
									  salesForm.form.findField('salePlace').setValue(json.place);
									  salesForm.form.findField('cargo.id').setValue(json.id);
									},
								params : 'query=' + record.get('name')
							});
				    }
				}
		}

		var button_add = new Ext.Button({
			text : '添加',
			disabled : true,
			handler : function() {

					progressConsole();
				if(salesForm.form.isValid()){ 	
					if (salesForm.form.findField("dt_Cargo.id").getValue() == "") {
						salesForm.form.submit({
						  url : '/weihe/addDTCargo_saleList',
						   success : function(form, action) {
							  if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
							  }else{
								Ext.Msg.alert('消息', action.result.msg, function() {
									 var i = salesForm.form.findField('saleList.id').getValue();
									   salesForm.form.reset();
									   salesForm.form.findField('saleList.id').setValue(i);
									   Ext.getCmp("sgrid").getStore().baseParams = {'saleList.id': i};
									   Ext.getCmp("sgrid").getStore().load({params : {start : 0, limit : 6}});
									
								});
							  }
						    },
							failure : function(form, action) {
								Ext.Msg.alert('错误', action.result.msg);
						    }
					    });
					}else {
						salesForm.form.submit({
							  url : '/weihe/updateDTCargo_saleList',
							   success : function(form, action) {
								  if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
								  }else{
									Ext.Msg.alert('消息', action.result.msg, function() {
										var i = salesForm.form.findField('saleList.id').getValue();
										salesForm.form.reset();
										 salesForm.form.findField('saleList.id').setValue(i);
										Ext.getCmp("sgrid").getStore().reload();
										    salesForm.buttons[0].setText('添加');
										
									});
								  }
							    },
								failure : function(form, action) {
									Ext.Msg.alert('错误', action.result.msg);
							    }
						    })
					}
			}else{
				Ext.Msg.alert('提示','请输入相关内容');
			}
				}
		});

	var button_clear = new Ext.Button({
		text : '清空',
		disabled : true,
		handler : function() {
		var i = salesForm.form.findField('saleList.id').getValue();
			salesForm.form.reset();
			salesForm.form.findField('saleList.id').setValue(i);
		    salesForm.buttons[0].setText('添加');
		}
	});

	var button_del = new Ext.Button({
		text : '删除',
		disabled : true,
		handler : function() {
			var id = salesForm.form.findField('dt_Cargo.id').getValue();
			if (id == '') {
				Ext.Msg.alert('提示', '请选择需要删除的信息!');
			} else {
				progressConsole();
				Ext.Ajax.request({
					url : '/weihe/deleteDTCargo_saleList',
					success : function(response) {
						var json = Ext.decode(response.responseText);
						if (json.success) {
							if (json.deny) {
									Ext.Msg.alert('错误',json.msg);
									var i = salesForm.form.findField('saleList.id').getValue();
									salesForm.form.reset();
									salesForm.form.findField('saleList.id').setValue(i);
									salesForm.buttons[0].setText('添加');
							}else{
							Ext.Msg.alert('消息', json.msg, function() {
								Ext.getCmp("sgrid").getStore().load({params : {start : 0, limit : 6}});
								var i = salesForm.form.findField('saleList.id').getValue();
								salesForm.form.reset();
								salesForm.form.findField('saleList.id').setValue(i);
								salesForm.buttons[0].setText('添加');
							});
						}
					   }
					},
					failure : function() {
						Ext.Msg.alert('错误', '删除失败!');
					},
					params : 'dt_Cargo.id=' + id
				});
			}
		}
	});
	
		var salesForm = new Ext.form.FormPanel({
			id : 'salesForm',
			title : '货物信息',
			frame : true,
			width : 300,
			region : 'east',
			autoScroll : true,
			collapseMode :'mini',
			collapsible : true,
			labelAlign : 'right',
			labelWidth : 60,
			defaultType : 'textfield',
				defaults : {
				width : 200
			},
			items : [{
				id: 'saleId',
				xtype : 'hidden',
				name : 'dt_Cargo.id'
			},{
				xtype : 'hidden',
				name : 'saleList.id'
			},{
				xtype : 'hidden',
				name : 'cargo.id'
			},codeCombo ,nameCombo
			 ,{
				id:'saleSpecs',
				fieldLabel : '规格型号',
				name : 'cargo.specs',
				readOnly: true
			},{
				id:'saleUnit',
				fieldLabel : '单位',
				name : 'cargo.unit',
				readOnly: true
			},{
				id:'salePlace',
				fieldLabel : '存放位置',
				name : 'cargo.place',
				readOnly: true
			},{
				id:'salePrice',
				fieldLabel : '单价',
				name : 'dt_Cargo.price',
				readOnly: true
			},{
				id:'saleAmount',
				fieldLabel : '数量',
				name : 'dt_Cargo.amount',
				regex : /^[1-9]\d*$/,
			    regexText:'请输入有效的数量',  
			    allowBlank:false,
				blankText:'请输入数量'
			},{
				id:'saleRemark',
				fieldLabel : '备注',
				name : 'dt_Cargo.remark'
			}],
			buttons : [button_add,button_clear,button_del]
		
	});
		return salesForm;
	}
  }
}();








